// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.datainterface.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.R;
import com.fowo.api.datainterface.entity.DatainterfaceMapping;
import com.fowo.api.datainterface.model.DatainterfaceMappingSearchParamPo;
import com.fowo.api.datainterface.model.DatainterfaceMappingVo;
import com.fowo.api.datainterface.service.DatainterfaceMappingService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "映射")
@RequestMapping("/api/datainterface-mapping")
@RestController
public class DatainterfaceMappingController {

  @Resource
  private DatainterfaceMappingService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<DatainterfaceMapping>> search(
    DatainterfaceMappingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
//      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(
    DatainterfaceMappingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
//      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<DatainterfaceMapping>> getPage(
    DatainterfaceMappingSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
//            search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<DatainterfaceMappingVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取事务编码相关的编号列表")
  @GetMapping("task-code/{texts}/ids")
  public R<List<Long>> getIdsByTaskCode(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByTaskCode(nameList));
  }

  @Operation(summary = "获取编号相关的事务编码列表")
  @GetMapping("task-code/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getTaskCodeByIds(idList));
  }

  @PreAuthorize("@am.canAccess('datainterfaceMappingCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody DatainterfaceMappingVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('datainterfaceMappingEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody DatainterfaceMappingVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('datainterfaceMappingEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody DatainterfaceMappingVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('datainterfaceMappingDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('datainterfaceMappingDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

}
